import React from "react";

const tagList = [];


export const tagReducer = (state=tagList, action) => {
    
    switch(action.type){
        case 'tagAdd':
            let menuList = action.menuList;
            let title = searchTitle(menuList,action.route.pathname);

            let newRoute = {
                ...action.route,
                title
            }

            // 重复添加
            const list = [...state]
            let isOk =list.some(item=>
                item.pathname.indexOf(action.route.pathname) !== -1
            )
            if(!isOk){
                return [...list,newRoute];
            }
            return list;
        default:
            return state;
    }
}
let title = '';
const searchTitle = (menuList,path)=>{
  
    menuList.forEach(menu => {
        if(menu.path == path){
            title =  menu.meta.title;  
            return;
        }
        if(menu.children){
            searchTitle(menu.children,path)
        } 
    })
    return title;
}